package com.cimpm.project.service.impl;

import com.cimpm.project.entity.Department;
import com.hby.common.callback.AutoProcessControllerPart;
import com.hby.common.entities.aspect.parameter.MapperParam;
import com.hby.common.entities.aspect.parameter.ParamConverter;
import com.hby.common.entities.aspect.parameter.ServiceInfo;
import com.hby.common.exceptions.AutoException;
import com.hby.common.exceptions.ExceptionEnum;
import com.hby.common.services.Impl.mian.AutoEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class DepartmentServiceImpl extends AutoEntityServiceImpl<Department> {

    @Autowired
    private ProjectdepartmentMapperServiceImpl projectdepartmentMapperService;

    @Override
    public void customService(AutoProcessControllerPart autoProcessControllerPart,ServiceInfo serviceInfo) throws Throwable {
        //添加
        autoProcessControllerPart.setBeforeAddPartCallback(() -> {
            if (!serviceInfo.getParamConverter().containsKey("projectId"))
                throw new AutoException(ExceptionEnum.PARAM_FIELD, "需要projectId");
            else {
                Integer projectId = serviceInfo.getParamConverter().getInt("projectId");
                Integer parentId = serviceInfo.getParamConverter().getInt("parentId");
                if (parentId == -1) return null;
                Set<Integer> departmentIds = projectdepartmentMapperService.getSecondaryIdsFromPrimaryId(projectId);
                if (!departmentIds.contains(parentId))
                    throw new AutoException(ExceptionEnum.PARAM_FIELD, "departmentId不在projectId下");
            }
            return null;
        });
        autoProcessControllerPart.setAfterAddPartCallback(() -> {
            Integer departmentId = serviceInfo.getNewId();
            projectdepartmentMapperService.addMapper(new MapperParam(serviceInfo.getParamConverter().getInt("projectId"), departmentId));
            return null;
        });
        //修改
        autoProcessControllerPart.setBeforePutPartCallback(() -> {
            serviceInfo.getParamConverter().getParamMap().remove("parentId");
            return null;
        });
        //修改
        autoProcessControllerPart.setBeforeDelPartCallback(() -> {
            List<Department> entitiesByOneField = getEntitiesFromOneField("parentId", serviceInfo.getParamConverter().getInt("id"));
            if (entitiesByOneField.size() > 0) throw new AutoException(ExceptionEnum.BEING_USED, "该部门下存在其他部门");
            return null;
        });
    }

    @Override
    public void addEntityOtherToRedis(Department department) {

    }

    @Override
    public void delEntityOtherFromRedis(Department department) {

    }

    public Object departmentTree(ParamConverter paramConverter, Integer userId, String userName) throws Throwable {
        int projectId = Integer.parseInt(paramConverter.getParamMap().get("projectId").toString());
        List<Department> departments = projectdepartmentMapperService.getSecondaryEntitiesFromPrimaryId(projectId);
        return getEntityTree(departments, -1);
    }

}
